﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using OF.Component.BLL;
using OF.Component.DTO;
using OF.Component.Model;
using OF.Utility;
using User.Api.Common;
using User.Api.Models;
using AutoMapper;

namespace User.Api.Controllers
{
    /// <summary>
    /// 用户邮寄地址管理
    /// </summary>
    public class MailAddressController : BaseApiController
    {

        /// <summary>
        /// 邮寄地址添加
        /// </summary>
        /// <param name="reqInfo">请求信息</param>
        /// <returns></returns>
        public ResultData<ResponseCommon<int>> Add(RequestMailAddressAdd reqInfo)
        {
            var result = ResultData<ResponseCommon<int>>.Error("");
            var oExistedUser = UserOperate.GetByID(reqInfo.UserID);
            if (oExistedUser == null)
            {
                throw new BizException(StatusCodeEnum.UserNoInfor);
            }
            if (!ValidateHelper.IsMobileNumber(reqInfo.MailUserMobile))
            {
                throw new BizException(StatusCodeEnum.UserMobileError);
            }
            //var oExisted = MailAddressOperate.QuerySingle(m => m.UserID==reqInfo.UserID&& m.MailUserName==reqInfo.MailUserName && m.MailUserMobile==reqInfo.MailUserMobile && m.MailAreaID==reqInfo.MailAreaID && m.MailAddress==reqInfo.MailAddress&&m.IsActive==1);
            //if (oExisted != null)
            //{
            //    throw new BizException(StatusCodeEnum.MailAddressExist);
            //}
            var oInfo = Mapper.Map<MailAddressInfo>(reqInfo);
            oInfo.CreatedDate = DateTime.Now;
            oInfo.UpdatedDate = DateTime.Now;
            oInfo.IsActive = 1;
            var addressID = MailAddressOperate.Insert(oInfo);
            return ResultData<ResponseCommon<int>>.Success(new ResponseCommon<int>(addressID));
        }

        /// <summary>
        /// 邮寄地址修改
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        /// <exception cref="BizException"></exception>
        public ResultData<ResponseCommon<int>> Update(RequestMailAddressUpdate reqInfo)
        {
            var result = ResultData<ResponseCommon<int>>.Error("");
            var oHasThisAddress = MailAddressOperate.GetByID(reqInfo.MailAddressID);
            if (oHasThisAddress == null)
            {
                throw new BizException(StatusCodeEnum.MailAddressNoInfor);
            }
            var oExistedUser = UserOperate.GetByID(reqInfo.UserID);
            if (oExistedUser == null)
            {
                throw new BizException(StatusCodeEnum.UserNoInfor);
            }
            if (!ValidateHelper.IsMobileNumber(reqInfo.MailUserMobile))
            {
                throw new BizException(StatusCodeEnum.UserMobileError);
            }
            //var oExisted = MailAddressOperate.QuerySingle(m => m.MailAddressID==reqInfo.MailAddressID &&  m.UserID == reqInfo.UserID && m.MailUserName == reqInfo.MailUserName && m.MailUserMobile == reqInfo.MailUserMobile && m.MailAreaID == reqInfo.MailAreaID && m.MailAddress == reqInfo.MailAddress &&m.IsActive==1);
            //if (oExisted != null)
            //{
            //    throw new BizException(StatusCodeEnum.MailAddressExist);
            //}
            var oInfo = Mapper.Map<MailAddressInfo>(reqInfo);
            oInfo.CreatedDate = DateTime.Now;
            oInfo.UpdatedDate = DateTime.Now;
            oInfo.IsActive = 1;
            MailAddressOperate.Update(oInfo);
            return ResultData<ResponseCommon<int>>.Success(new ResponseCommon<int>(reqInfo.MailAddressID));
        }

        /// <summary>
        /// 删除接口
        /// </summary>
        /// <param name="mailAddressID">邮寄地址ID</param>
        /// <returns></returns>
        public ResultData<ResponseCommon<int>> Remove(int mailAddressID)
        {
            var result = ResultData.Error("");
            var oExist = MailAddressOperate.GetByID(mailAddressID);
            if (oExist == null)
            {
                throw new BizException(StatusCodeEnum.MailAddressNoInfor);
            }
            MailAddressOperate.Delete(mailAddressID);
            return ResultData<ResponseCommon<int>>.Success(new ResponseCommon<int>(mailAddressID));
        }

        /// <summary>
        /// 获取邮寄地址信息
        /// </summary>
        /// <param name="mailAddressID">邮寄地址ID</param>
        /// <returns></returns>
        /// <exception cref="BizException"></exception>
        public ResultData<ResponseMailAddressGet> Get(int mailAddressID)
        {
            var result = ResultData<ResponseMailAddressGet>.Error("");
            var oExist = MailAddressOperate.GetByID(mailAddressID);
            if (oExist == null)
            {
                throw new BizException(StatusCodeEnum.MailAddressNoInfor);
            }
            var oInfo = Mapper.Map<ResponseMailAddressGet>(oExist);
            BaseLocation_AllInfo baseInfo = BaseLocation_AllOperate.QuerySingle(a => a.locationId == oInfo.MailAreaID);
            if (baseInfo!=null)
            {
                oInfo.AreaName = string.Format("{0}{1}{2}", baseInfo.pName, baseInfo.cName,baseInfo.locationName);
            }            
            result = ResultData<ResponseMailAddressGet>.Success(oInfo);
            return result;
        }

        /// <summary>
        /// 获取默认的邮寄地址
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ResultData<ResponseMailAddressGet> GetDefault(long userId)
        {
            var result = ResultData<ResponseMailAddressGet>.Error("");
            var oExist = MailAddressOperate.GetDefault(userId);
            if (oExist == null)
            {
                var oInfo = Mapper.Map<ResponseMailAddressGet>(oExist);
                result = ResultData<ResponseMailAddressGet>.Success("没有默认邮寄地址");
            }
            else
            {
                var oInfo = Mapper.Map<ResponseMailAddressGet>(oExist);
                BaseLocation_AllInfo baseInfo = BaseLocation_AllOperate.QuerySingle(a => a.locationId == oInfo.MailAreaID);
                if (baseInfo != null)
                {
                    oInfo.AreaName = string.Format("{0}{1}{2}", baseInfo.pName, baseInfo.cName, baseInfo.locationName);
                }
                result = ResultData<ResponseMailAddressGet>.Success(oInfo);
            }

            return result;
        }

        /// <summary>
        /// 邮寄地址信息列表获取
        /// </summary>
        /// <param name="con">查询条件</param>
        /// <returns></returns>
        public ResultData<ResponseMailAddressGetList> GetList([FromUri] scMailAddress con)
        {
            IList<BaseLocation_AllInfo> baseList = BaseLocation_AllOperate.QueryList(a => a.locationId > 0);
            var result = ResultData<ResponseMailAddressGetList>.Error("");
            if (con.rows == 0)
            {
                con.rows = 100;
            }
            if (con.page == 0)
            {
                con.page = 1;
            }
            if (con.UserID > 0)
            {
                int total;
                var list = MailAddressOperate.SearchList(con, out total);
                var listResult = new List<ResponseMailAddressGet>();
                list.ForEach(m =>
                {
                    var oInfo = Mapper.Map<ResponseMailAddressGet>(m);
                    var maList = baseList.Where(a => a.locationId == oInfo.MailAreaID).ToList();
                    if (maList.Count>0)
                    {
                        oInfo.AreaName = string.Format("{0}{1}{2}", maList[0].pName, maList[0].cName, maList[0].locationName);
                    }
                    //此处应该还需要调用城市接口获取城市信息
                    listResult.Add(oInfo);
                });
                result = ResultData<ResponseMailAddressGetList>.Success(new ResponseMailAddressGetList
                {
                    items = listResult,
                    total = total
                });
            }
            return result;
        }


    }
}